package com.fuxian.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fuxian.model.Lessonclassifcation;
import com.fuxian.model.Lessonitem;
import com.fuxian.model.Prospectivestudent;
import com.fuxian.service.LessonService;
import com.fuxian.service.LessonclassifService;
import com.fuxian.service.ProspectivestudentService;
import com.fuxian.util.DateUtile;
import com.fuxian.util.ReturnMapUtil;

@Controller
@Scope("prototype")
public class LessonAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(LessonAction.class);

	@Resource
	private LessonService lessonService;

	@Resource
	private LessonclassifService lessonclassifService;

	@Resource
	private ProspectivestudentService onLineService;

	/* =============================== */
	private int rows;
	private int page;

	/* =====Lessonclassifcation======== */
	private int classif_id;
	private String name;
	private String description;

	/* ================================== */
	private String hour;
	private String price;
	private String grade;
	private String photo_path;

	/* ================================== */

	/* ================================ */
	private int lesson_id;
	private int teacher_id;

	/* =============================== */

	private String school;// 学校
	private String phone;// 电话
	private String qq_num;// QQ号码
	private String email;// 邮箱地址
	

	/* ================================= */

	public void setClassif_id(int classifId) {
		classif_id = classifId;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setLesson_id(int lessonId) {
		lesson_id = lessonId;
	}

	public void setTeacher_id(int teacherId) {
		teacher_id = teacherId;
	}

	public void setHour(String hour) {
		this.hour = hour;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	/* =============================== */

	public void setPhoto_path(String photoPath) {
		photo_path = photoPath;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setQq_num(String qqNum) {
		qq_num = qqNum;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLessonClassif() {
		returnMap = new HashMap<String, Object>();
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		page = ((page == 0) ? 1 : page);
		rows = ((rows == 0) ? 1 : rows);
		int totalCount = lessonclassifService.selectCount();
		int max_page = (totalCount + rows - 1) / rows;
		returnMap.put("max_page", max_page);
		returnMap.put("page", page);
		returnMap.put("total", totalCount);
		List<Lessonclassifcation> lessonClassif = lessonclassifService
				.selectAlllessonclassifcation(rows * (page - 1), rows);
		returnMap.put("rows", lessonClassif);
		return SUCCESS;
	}

	public String getLessonClassifById() {
		returnMap = new HashMap<String, Object>();
		Lessonclassifcation lessonClassif = lessonclassifService
				.selectLessonclassifcation(classif_id);
		returnMap.put("rows", lessonClassif);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String addLessonClssif() {
		returnMap = new HashMap<String, Object>();
		// 加载验证session验证

		Lessonclassifcation lessonClassif = new Lessonclassifcation();
		lessonClassif.setName(name);
		lessonClassif.setDescription(description);
		lessonClassif.setCreate_time(DateUtile.getTimestamp());
		lessonClassif.setCreate_user((String) session.get("user_name"));
		lessonclassifService.addLessonclassifcation(lessonClassif);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String updateLessonClassif() {
		returnMap = new HashMap<String, Object>();
		Lessonclassifcation lessonClassif = new Lessonclassifcation();
		lessonClassif.setClassif_id(classif_id);
		lessonClassif.setName(name);
		lessonClassif.setDescription(description);
		lessonclassifService.modifyLessonclassifcation(lessonClassif);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String deleteLessonClassif() {
		returnMap = new HashMap<String, Object>();
		lessonclassifService.deleteLessonclassifcationById(classif_id);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String getLessonInfo() {
		returnMap = new HashMap<String, Object>();
		page = ((page == 0) ? 1 : page);
		rows = ((rows == 0) ? 1 : rows);
		// 获取总数
		int totalrows = lessonService.selectCount(-1);
		int max_page = (totalrows + rows - 1) / rows;
		returnMap.put("max_page", max_page);
		returnMap.put("page", page + 1);
		returnMap.put("total", totalrows);
		List<Lessonitem> lessonList = lessonService.selectAlllLessonItem(rows
				* (page - 1), rows);
		returnMap.put("rows", lessonList);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String getLessonInfoByClassifID() {
		returnMap = new HashMap<String, Object>();
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		// 获取总数
		int totalrows = lessonService.selectCount(classif_id);
		int max_page = (totalrows + rows - 1) / rows;
		returnMap.put("max_page", max_page);
		returnMap.put("page", page);
		returnMap.put("total", totalrows);
		returnMap.put("classif_id", classif_id);
		List<Lessonitem> lessonList = lessonService
				.selectLessonItemByClassifId(classif_id);
		returnMap.put("rows", lessonList);
		return SUCCESS;
	}

	public String getLessonInfoByLessonID() {
		returnMap = new HashMap<String, Object>();
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		Lessonitem lesson = lessonService.selectLessonItemByLessonId(lesson_id);
		returnMap.put("name", lesson.getName());
		returnMap.put("houre", lesson.getHour());
		returnMap.put("grade", lesson.getGrade());
		returnMap.put("price", lesson.getPrice());
		returnMap.put("description", lesson.getDescription());
		System.out.println(lesson.getDescription());
		returnMap.put("photo_path", lesson.getPhoto_path());
		return SUCCESS;
	}

	public String getLessonInfoByTeacherID() {
		returnMap = new HashMap<String, Object>();
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		List<Lessonitem> lessonList = lessonService
				.selectLessonItemByTeacherId(teacher_id);
		returnMap.put("rows", lessonList);
		return SUCCESS;
	}

	public String addLesson() {
		returnMap = new HashMap<String, Object>();
		Lessonitem lesson = new Lessonitem();
		lesson.setLesson_id(lesson_id);
		lesson.setClassif_id(classif_id);
		lesson.setName(name);
		lesson.setHour(hour);
		lesson.setPrice(price);
		lesson.setGrade(grade);
		lesson.setPhoto_path(photo_path);
		lesson.setDescription(description);
		lessonService.addLessonItem(lesson);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String updateLesson() {
		returnMap = new HashMap<String, Object>();
		Lessonitem lesson = new Lessonitem();
		lesson.setLesson_id(lesson_id);
		lesson.setClassif_id(classif_id);
		lesson.setName(name);
		lesson.setHour(hour);
		lesson.setPrice(price);
		lesson.setGrade(grade);
		lesson.setDescription(description);
		lessonService.modifyLessonItem(lesson);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String deleteLesson() {
		returnMap = new HashMap<String, Object>();
		lessonService.deleteLessonItemById(lesson_id);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

	public String applyOnline() {
		returnMap = new HashMap<String, Object>();
		Prospectivestudent onLineModel = new Prospectivestudent();
		onLineModel.setName(name);
		onLineModel.setSchool(school);
		onLineModel.setGrade(grade);
		onLineModel.setPhone(phone);
		onLineModel.setQq_num(qq_num);
		onLineModel.setEmail(email);
		onLineModel.setLesson_id(lesson_id);
		onLineModel.setCreate_user((String) session.get("user_name"));
		onLineService.addProspectivestudent(onLineModel);
		returnMap = ReturnMapUtil.setReturnHeader(returnMap, true, "成功！");
		return SUCCESS;
	}

}
